<?php

require_once 'classes/LightPressPlugin.php';

class TemplateInclude extends LightPressPlugin {
    
    var $default_context = LP_CONTEXT_ALL;
    var $active = true;
    var $hooks = array('post_render');

    var $description = 'Include one or more custom templates (similar to {SIDEBAR} )';
    
    var $constructor_args = array('templates' => 'List of template tags to be replaced with xml files (e.g. to replace {MYTEMPLATE} with the contents of mytemplate.xml). Separate multiple tags with a space, but don\'t include the {} braces or ".xml". The tags must be uppercase when used in your templates.');
    
    var $templates = 'mytemplate';
    
    var $_frontend;
    
    function TemplateInclude(&$frontend, $args, $dummy_run=false) {
        $this->LightPressPlugin($frontend, $args, $dummy_run);
    }
    
    function run($hook, &$payload) {
        $tpl =& $this->_frontend->tpl;
        $files = array();
        foreach (explode(' ', $this->templates) as $i=>$tag) {
            // TODO - check if files exist (or make template class handle this gracefully)
            $files[strtolower($tag)] = strtolower($tag) . '.xml';
        }
        $tpl->setFile($files);
        foreach ($files as $tag => $file)
            $tpl->parse(strtoupper($tag), strtolower($tag));
    }
    
    function hide() {
        $tpl =& $this->_frontend->tpl;
        $files = array();
        foreach (explode(' ', $this->templates) as $i=>$tag) {
            $tpl->setVar(strtoupper($tag), '');
        }
    }
}

?>